package com.xiao.hao.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiao.hao.constant.NumberConstants;
import com.xiao.hao.convertor.XhAllConfigConvertor;
import com.xiao.hao.entity.XhAllConfig;
import com.xiao.hao.facade.XhAllConfigFacade;
import com.xiao.hao.mapper.XhAllConfigMapper;
import com.xiao.hao.req.XhAllConfigInsertReq;
import com.xiao.hao.req.XhAllConfigQueryPageReq;
import com.xiao.hao.req.XhAllConfigUpdateReq;
import com.xiao.hao.utils.BaseId;
import com.xiao.hao.utils.PageResult;
import com.xiao.hao.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @Description 全局配置 服务实现层
 * @Author xiaohao
 * @Date 2025-03-04 14:08:20
 */
@Service
@Slf4j
public class XhAllConfigServiceImpl implements XhAllConfigFacade {

    @Resource
    XhAllConfigMapper mapper;


    /**
     * 功能描述: 角色新增
     *
     * @param req
     * @return com.xiao.hao.utils.Result
     * @author xiaohao
     * @date 2025-03-04 14:08:20
     */
    @Override
    public Result insert(XhAllConfigInsertReq req) {
        return Result.succeed(mapper.insert(XhAllConfigConvertor.INSTANCE.reqToEntity(req)));
    }


    /**
     * 功能描述: 角色修改
     *
     * @param req
     * @return com.xiao.hao.utils.Result
     * @author xiaohao
     * @date 2025-03-04 14:08:20
     */
    @Override
    public Result update(XhAllConfigUpdateReq req) {
        return Result.succeed(mapper.updateById(XhAllConfigConvertor.INSTANCE.reqToEntity(req)));
    }


    /**
     * 功能描述: 删除角色
     *
     * @param req
     * @return com.xiao.hao.utils.Result
     * @author xiaohao
     * @date 2025-03-04 14:08:20
     */
    @Override
    public Result delete(BaseId req) {
        return Result.succeed(mapper.deleteById(req.getId()));
    }

    /**
     * 功能描述: 角色分页查询
     *
     * @param req
     * @return com.xiao.hao.utils.PageResult
     * @author xiaohao
     * @date 2025-03-04 14:08:20
     */
    @Override
    public PageResult queryPage(XhAllConfigQueryPageReq req) {
        Page<XhAllConfig> page = mapper.selectPage(new Page(req.getNum(), req.getSize()), new LambdaQueryWrapper<XhAllConfig>().orderByDesc(XhAllConfig::getCreateTime));
        return PageResult.result(page.getTotal(), XhAllConfigConvertor.INSTANCE.entityListToRespList(page.getRecords()));
    }

    @Override
    public Result configSwitch() {
        XhAllConfig xhAllConfig = mapper.selectOne(new LambdaQueryWrapper<XhAllConfig>());
        xhAllConfig.setConfig(xhAllConfig.getConfig().equals(NumberConstants.INT_ONE) ? NumberConstants.INT_ZERO : NumberConstants.INT_ONE);
        return Result.succeed(mapper.updateById(xhAllConfig));
    }

}



